package AiFunctions
{
	import mx.collections.ArrayCollection;

	public class Ai
	{
		import grid.Topology;
		
		public var placedUnits:Array = new Array();
		public var selectedUnits = new Array();
		public var gridPos = new Array();
		public var rawCoords = new Array();
		public function Ai()
		{
		}
		/**
		 * Checks to see if the selected point intersects with something else
		 * then passes the coords to the function which produces the final coordinates, and saves them for future testing
		 * Runs this procedure as many times as the available units
		**/
		var geoProcess = new Topology();
		public function initAi(unit)
		{
			var Xpos = randomRange(1,10);
			var Ypos = randomRange(1,10);
			while ((geoProcess.intersects(no2unit(unit),{X:Xpos,Y:Ypos},gridPos)!=false)||
				((Xpos<1||Ypos<1)||(Xpos>10||Ypos>10)||
				(rawCoords.indexOf(String(Xpos)+String(Ypos))>-1)) )
			{
				Xpos = Math.round(Math.random()*10);
				Ypos = Math.round(Math.random()*10);
			}
			
			return {X:Xpos,Y:Ypos};
		}
		
		private function no2unit(unit)
		{
			switch(unit)
			{
				case 0:
					return "wraith";
					break;
				case 1:
					return "hammer";
					break;
				case 2:
					return "destroyer";
					break;
				case 3:
					return "killBike";
					break;
				case 4:
					return "oculus";
					break;
			}
		}
		
		public function autoSelectUnit(fn=false)
		{
			for(var i=0;i<5;i++)
			{
				var unit = Math.round(Math.random()*10);
				while (unit>4||selectedUnits.indexOf(unit)!=-1)
				{
					unit = Math.round(Math.random()*10);
				}
				//trace(selectedUnits.indexOf(unit));
				if(selectedUnits.indexOf(unit)==-1) {placedUnits.push(availableGridPos(unit,initAi(unit)));selectedUnits.push(unit);}
			}
			if(fn)fn(true);
			return placedUnits;
		}
		
		public function availableGridPos(selectedUnit,coords)
		{
			// 0 : Wraith
			// 1 : Hammer
			// 2 : Destroyer
			// 3 : killBike
			// 4 : Oculus
			switch (selectedUnit)
			{
				case 0:
						gridPos.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X,Y:coords.Y+2}]);
						rawCoords.push(String(coords.X)+String(coords.Y));
						rawCoords.push(String(coords.X)+String(coords.Y+1));
						rawCoords.push(String(coords.X)+String(coords.Y+2));
						return [{X:coords.X,Y:coords.Y,unit:no2unit(selectedUnit)},
							    {X:coords.X,Y:coords.Y+1,unit:no2unit(selectedUnit)},
							    {X:coords.X,Y:coords.Y+2,unit:no2unit(selectedUnit)}];
					break;
				case 1:
						gridPos.push([{X:coords.X,Y:coords.Y},
							{X:coords.X,Y:coords.Y+1},
							{X:coords.X+1,Y:coords.Y},
							{X:coords.X+1,Y:coords.Y+1}]);
						rawCoords.push(String(coords.X)+String(coords.Y));
						rawCoords.push(String(coords.X)+String(coords.Y+1));
						rawCoords.push(String(coords.X+1)+String(coords.Y+1));
						rawCoords.push(String(coords.X+1)+String(coords.Y+1));
						return [{X:coords.X,Y:coords.Y,unit:no2unit(selectedUnit)},
							    {X:coords.X,Y:coords.Y+1,unit:no2unit(selectedUnit)},
							    {X:coords.X+1,Y:coords.Y,unit:no2unit(selectedUnit)},
							    {X:coords.X+1,Y:coords.Y+1,unit:no2unit(selectedUnit)}];
					break;
				case 2:
						gridPos.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X,Y:coords.Y+2},{X:coords.X-1,Y:coords.Y+1},{X:coords.X+1,Y:coords.Y+1}]);
						rawCoords.push(String(coords.X)+String(coords.Y));
						rawCoords.push(String(coords.X)+String(coords.Y+1));
						rawCoords.push(String(coords.X)+String(coords.Y+2));
						rawCoords.push(String(coords.X-1)+String(coords.Y+1));
						rawCoords.push(String(coords.X+1)+String(coords.Y+1));
						return [{X:coords.X,Y:coords.Y,unit:no2unit(selectedUnit)},
							    {X:coords.X,Y:coords.Y+1,unit:no2unit(selectedUnit)},
							    {X:coords.X,Y:coords.Y+2,unit:no2unit(selectedUnit)},
							    {X:coords.X-1,Y:coords.Y+1,unit:no2unit(selectedUnit)},
							    {X:coords.X+1,Y:coords.Y+1,unit:no2unit(selectedUnit)}];
					break;
				case 3:
						gridPos.push([{X:coords.X,Y:coords.Y},{X:coords.X+1,Y:coords.Y+1}]);
						rawCoords.push(String(coords.X)+String(coords.Y));
						rawCoords.push(String(coords.X+1)+String(coords.Y+1));
						return [{X:coords.X,Y:coords.Y,unit:no2unit(selectedUnit)},
							    {X:coords.X+1,Y:coords.Y+1,unit:no2unit(selectedUnit)}];
					break;
				case 4:
						gridPos.push([{X:coords.X,Y:coords.Y},{X:coords.X,Y:coords.Y+1},{X:coords.X-1,Y:coords.Y},{X:coords.X+1,Y:coords.Y}]);
						rawCoords.push(String(coords.X)+String(coords.Y));
						rawCoords.push(String(coords.X)+String(coords.Y+1));
						rawCoords.push(String(coords.X-1)+String(coords.Y));
						rawCoords.push(String(coords.X+1)+String(coords.Y));
						return [
							{X:coords.X,Y:coords.Y,unit:no2unit(selectedUnit)},
							{X:coords.X,Y:coords.Y+1,unit:no2unit(selectedUnit)},
							{X:coords.X-1,Y:coords.Y,unit:no2unit(selectedUnit)},
							{X:coords.X+1,Y:coords.Y,unit:no2unit(selectedUnit)}];
					 break;
			}
		}
		
		public function removeAssets()
		{
			placedUnits = new Array();
			rawCoords = new Array();
			selectedUnits = new Array();
			gridPos = new Array();
		}
		
		function randomRange(minNum:Number=0, maxNum:Number=11):Number
		{
			return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
		}
	}
}